package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.BaseNumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;



    // 添加套餐 和添加套餐所属菜品
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //1.创建Setmeal对象，把SetmealDTO数据复制到该对象中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //2.调用setmealMapper往setmeal表中添加数据
        setmealMapper.insert(setmeal);

        //3.添加套餐的菜品数据到setmeal_dish表中
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        //4.调用setmealDishMapper方法添加套餐菜品数据到setmeal_dish表
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.开启分页 , 设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //2.查询setmeal表 ,对分页后的数据进行筛选
        Page<SetmealVO> page =  setmealMapper.page(setmealPageQueryDTO);
        //3.创建PageResult对象
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());

        return pageResult;
    }

    @Override
    public SetmealVO findSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.findSetmealById(id);
        log.info("====根据套餐id查询套餐详情，查询结果：{}",setmealVO);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //1.需改setmeal表中的数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.update(setmeal);

        //2.修改setmeal_dish表中的数据
        //2.1 先删除setmeal_dish表同一个套餐的所有菜品
        setmealDishMapper.delete(setmeal.getId());

        //2.2 再往setmeal_dish表中添加这个套餐对应的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public void delete(Long[] ids) {
        //1.先删除setmeal表中的数据
        setmealMapper.delete(ids);

        //2.再删除setmeal_dish表中的数据
        for (Long id : ids) {
            setmealDishMapper.delete(id);
        }
    }

    @Override
    public void update(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);

        setmealMapper.update(setmeal);
    }

    @Override
    public List<Setmeal> findSetmealByCategoryId(long categoryId) {
        List<Setmeal> list = setmealMapper.findSetmealByCategoryId(categoryId);
        return list;
    }

    @Override
    public List<DishItemVO> findDishBySetmealId(long id) {
        //查询setmeal_dish和dish表，获取套餐对应的菜品数据
        List<DishItemVO> list = setmealDishMapper.findDishItemVOBySetmealId(id);
        return list;
    }
}
